import { generateHtmlContent } from "./templates";
import { getIconfontContent, getOutputPath } from "./tools";
import { IOptions, ISvgOptions } from "./types";
const fs = require("fs");
const path = require("path");

export function generateHtml(option: IOptions) {
  const { inputValue = "" } = option;
  const inputPathList = inputValue.split("/").filter(Boolean);
  const inputPath = path.join(process.cwd(), ...inputPathList);

  if (!fs.statSync(inputPath).isFile() && !inputValue.endsWith(".js")) {
    throw new Error("input should be a .js file");
  }
  inputPathList.pop();
  option.outputValue = option.outputValue || inputPathList.join("/");
  const { svgList } = getIconfontContent(inputPath);
  outputFile(svgList, option);
}

// 输出index.html文件
function outputFile(svgList: ISvgOptions[], options: IOptions) {
  const { outputValue } = options;
  const outputPath = getOutputPath(outputValue, "index.html", ".html");
  const html = generateHtmlContent(svgList.map((item) => item.name));
  fs.writeFileSync(outputPath, html);
}
